#!/bin/bash
# N.B.: Use bash for $RANDOM
#
#    mondrian: randomly generate some Modrian art in tmux
#
#    Copyright (C) 2011 Dustin Kirkland
#
#    Authors: Dustin Kirkland <kirkland@byobu.co>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

trap "tput reset" EXIT HUP INT QUIT TERM KILL ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM

# Special mode, argv[1]="color", paint the screen
if [ "$1" = "color" ] && [ -n "$2" ]; then
	i=0
	out=
	count=$(stty size | awk '{print $1*$2}')
	perl -e "print color 'reset'; printf \"\x1b[48;5;${2}m%${count}s\", \" \";";
	head -n1
	exit 0
fi

get_random_pane() {
	local panes=$(tmux list-panes | wc -l)
	r=$((RANDOM % 4))
	while [ $r = "$LAST_PANE" ]; do
		r=$((RANDOM % 4))
	done
	LAST_PANE="$r"
	printf "%s\n" "$r"
}

get_random_numbers() {
	local max="$1" R=0 B=0 Y=0
	R=$((RANDOM % max + 1))
	B=$((RANDOM % max + 1))
	Y=$((RANDOM % max + 1))
	while [ "$B" = "$R" ] || [ "$B" = "$Y" ]; do
		B=$((RANDOM % max + 1))
	done
	while [ "$Y" = "$R" ] || [ "$Y" = "$B" ]; do
		Y=$((RANDOM % max + 1))
	done
	_RET="$R $B $Y"
}

red_color=196
yellow_color=226
blue_color=57
white_color=231

# Get number of splits
if [ -n "$1" ]; then
	# Passed in as arg1
	TOTAL_SPLITS=$1
else
	# Choose random number of splits between 10 and 40
	TOTAL_SPLITS=$((RANDOM % 30 + 10))
fi

# Choose 1 red, 1 yellow, 1 blue
get_random_numbers "$TOTAL_SPLITS"
nums="$_RET"
red=$((RANDOM % TOTAL_SPLITS + 4))
while [ $red -ge $TOTAL_SPLITS ]; do
	red=$((RANDOM % TOTAL_SPLITS + 4))
done
yellow=$((RANDOM % TOTAL_SPLITS + 4))
while [ "$yellow" = "$red" ] || [ $yellow -ge $TOTAL_SPLITS ]; do
	yellow=$((RANDOM % TOTAL_SPLITS + 4))
done
blue=$((RANDOM % TOTAL_SPLITS + 4))
while [ "$blue" = "$yellow" ] || [ "$blue" = "$red" ] || [ $blue -ge $TOTAL_SPLITS ]; do
	blue=$((RANDOM % TOTAL_SPLITS + 4))
done

# Create the window, set the border colors
for i in $(tmux list-windows | grep " mondrian " | awk -F: '{print $1}'); do
	tmux kill-window -t $i
done
tmux new-window -n mondrian "$BYOBU_PREFIX/lib/byobu/include/mondrian color $white_color"
tmux set-option -g pane-active-border-bg black
tmux set-option -g pane-active-border-fg black
tmux set-option -g pane-border-bg black
tmux set-option -g pane-border-fg black

# Create and paint the splits
for i in $(seq 0 $((TOTAL_SPLITS-1))); do
	case $i in
		$red) color=$red_color ;;
		$yellow) color=$yellow_color ;;
		$blue) color=$blue_color ;;
		*) color=$white_color ;;
	esac
	CMD="$BYOBU_PREFIX/lib/byobu/include/mondrian color $color"
	[ "$((RANDOM % 2))" = "0" ] && SPLIT="-v" || SPLIT="-h"
	if ! tmux split-window $SPLIT -t $(get_random_pane) "$CMD" ; then
		case "$i" in
			$red) red=$((red+1)) ;;
			$yellow) yellow=$((yellow+1)) ;;
			$blue) blue=$((blue+1)) ;;
		esac
	fi
done
exit 0
